package com.gy.sc.scconsumermovie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.gy.sc.scconsumermovie.entity.TUser;
import com.gy.sc.scconsumermovie.remote.UserRemote;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;

    // @Value("${user.userServiceUrl}")
    // private String userServiceUrl;

    @Autowired
    private UserRemote userRemote;

    @GetMapping("movie/get/user/{id}")
    public TUser findAUserById(@PathVariable("id") Integer id) {
        TUser tUser = userRemote.getUserById(1);
        System.out.println(tUser);
        return tUser;
    }

    @GetMapping("movie/get/user2/{id}")
    @HystrixCommand(fallbackMethod = "findAUserByIdFallBack")
    public TUser findAUserById2(@PathVariable("id") Integer id) {
        // 服务名称必须用大写！！！
        TUser tUser2 = restTemplate.getForObject(
                "http://SC-PROVIDER-USER/get/user/" + id, TUser.class);
        System.out.println(tUser2);
        return tUser2;
    }

    /**
     * @author GY
     * @date 2018年11月15日
     * @说明: Hystrix 回退方法
     */
    public TUser findAUserByIdFallBack(Integer id) {
        return null;
    }

}
